package cn.cqie.frame.controller;


import cn.cqie.frame.bo.add.AddPermissionsBo;
import cn.cqie.frame.bo.query.QueryRoleBo;
import cn.cqie.frame.bo.update.UpdatePermissionsBo;
import cn.cqie.frame.service.SysRoleService;
import cn.cqie.frame.utils.MyPageHelper;
import cn.cqie.frame.vo.PermissionsVo;
import cn.cqie.frame.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHL
 * @since 2021-09-14
 */
@RestController
@Api("角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService ;

    /**
     * 查询角色信息(分页)
     * @param bo
     * @return
     */
    @ApiOperation("查询角色信息(分页)")
    @GetMapping(value = "/permissions/getPermissionsList")
    @ResponseBody
    public ResultVo<PageInfo<PermissionsVo>>getPermissionsList(QueryRoleBo bo) {
        return ResultVo.success(MyPageHelper.returnPage(sysRoleService.getPermissionsList(bo)));
    }

    /**
     * 查询角色信息
     * @return
     */
    @ApiOperation("查询角色信息")
    @GetMapping(value = "/permissions/getUserPermissionsList")
    public ResultVo<PageInfo<PermissionsVo>>getUserPermissionsList() {
        List<PermissionsVo> userPermissionsList = sysRoleService.getUserPermissionsList();
        return ResultVo.success(MyPageHelper.returnPage(userPermissionsList));
    }

    /**
     * 添加/更新角色信息
     * @param bo
     * @return
     */
    @ApiOperation("添加/更新角色信息")
    @PostMapping(value = "/permissions/addPermissions")
    @ResponseBody
    public ResultVo<Void> saveOrUpdateRole(@Valid @RequestBody AddPermissionsBo bo) {
         sysRoleService.saveOrUpdateRole(bo);
        return ResultVo.success();
    }

    /**
     * 更新角色信息
     * @param bo
     * @return
     */
    @ApiOperation("更新角色信息")
    @PutMapping(value = "/permissions/updatePermissions")
    @ResponseBody
    public ResultVo<Void> updatePermissions(@RequestBody UpdatePermissionsBo bo) {
        Boolean aBoolean = sysRoleService.updatePermissions(bo);
        return aBoolean ? ResultVo.success() : ResultVo.error("更新失败");
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @ApiOperation("删除角色")
    @DeleteMapping("/permissions/deleterPermissions/{roleId}")
    public ResultVo<Void> deletePermissions(@PathVariable Integer roleId) {
        Boolean aBoolean = sysRoleService.deletePermissions(roleId);
        return aBoolean ? ResultVo.success() : ResultVo.error("删除失败");
    }

}

